package qqtalk.jiemian;

import javax.swing.*;

import qqtalk.gonggong.NetPrintStream;
//import com.alee.laf.WebLookAndFeel;
import qqtalk.gonggong.Message;
import qqtalk.gonggong.MessageType;
import qqtalk.gonggong.User;
import qqtalk.gongju.Glhylb;
import qqtalk.gongju.Glxc;
import qqtalk.jiemian1.liebiao;
import qqtalk.moshi.*;
import qqtalk.user.Member;
import qqtalk.user.MemberManager;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import sun.audio.*;

import static java.awt.Color.pink;

public class Jiemian
{

    private JTextField hostname_Text;
    private JTextField prt_Text;
    private JButton link_Btn;
    private JTextField sendText;
    private JButton send_Btn;
    private JTextArea show_Mes;
    private JPanel Jpanel1;
    private JList user_list;
    private JLabel hostname;
    private JLabel port;
    public List<String> userlist;

    ServerSocket ss;
    Socket s;
    static liebiao lb;


    public Jiemian() {

//        show_Mes.setBackground(pink);
        show_Mes.setEditable(false);
        userlist=new ArrayList<String>(1024);
//        show_Mes.setBackground(Color.getHSBColor(255,239,241));

//        Jpanel1 = new JPanel() {
//            public void paintComponent(Graphics g) {
//                super.paintComponent(g);
//                ImageIcon ii = new ImageIcon("C:\\Users\\浅汐\\Desktop\\image\\background.jpg");
////                g.drawImage(ii.getImage(), 0, 0, getWidth(), getHeight(), ii.getImageObserver());
//                g.drawImage(ii.getImage(), 0, 0, getWidth(), getHeight(), null);
//            }
////            public void paintComponent(Graphics g) {
////                super.paintComponent(g);
////                ImageIcon ii = new ImageIcon("C:\\\\Users\\\\浅汐\\\\Desktop\\\\image\\\\background.jpg");
////                g.drawImage(ii.getImage(), 0, 0, 500, 500, this);
//////                JPanel.instance().repaint();
////            }
//        };

        link_Btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                NetPrintStream stream = new NetPrintStream(System.out, show_Mes);
                System.setErr(stream);
                System.setOut(stream);
                if(e.getSource()==link_Btn)
                {
//                    new Fuwu();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                    ss=new ServerSocket(9999);
                                    System.out.println("服务器正在监听....");
                                while(true)
                                {
                                    System.out.println("dsafadsfdsgvdsbvsdf");
                                    s=ss.accept();
                                    System.out.println("客户机连接正在连接...");
                                    ObjectInputStream ois=new ObjectInputStream(s.getInputStream());
                                    User u=(User)ois.readObject();
                                    Message m=new Message();
                                    ObjectOutputStream oos=new ObjectOutputStream(s.getOutputStream());
                                    Member msg=new Member();
                                    msg.setUserId(u.getUserId());
                                    msg.setPasswd(u.getPasswd());
                                    MemberManager memberManager=new MemberManager();
                                    if(u.getPasswd().equals("123123"))
                                    {
                                        m.setMesType("1");
                                        oos.writeObject(m);
                                        Xiancheng xc=new Xiancheng(s,show_Mes,userlist);
                                        Xianchengbaioshi.addxiancheng(u.getUserId(), xc);
                                        xc.start();
                                        xc.tongzhi(u.getUserId());
                                        //上线声音
                                        //playSound("C:\\Users\\cheny\\IdeaProjects\\Communication\\src\\qqtalk\\sound\\denglu.wav");
                                        show_Mes.append(u.getUserId()+"客户机连接成功");
                                        userlist.add(u.getUserId());
                                        //System.out.println(u.getUserId()+"客户机连接成功");
                                        lb.upateFriend(u.getUserId());
                                    }
                                    else
                                    {
                                        m.setMesType("2");
                                        oos.writeObject(m);
                                        s.close();
                                    }
//                                    if(!memberManager.userLogin(msg))
//                                    {
//                                        m.setMesType("2");
//                                        oos.writeObject(m);
//                                        s.close();
//                                        JOptionPane.showMessageDialog(null,"失败");
//                                    }
//                                    else
//                                    {
//                                        m.setMesType("1");
//                                        oos.writeObject(m);
//                                        Xiancheng xc=new Xiancheng(s,show_Mes,userlist);
//                                        Xianchengbaioshi.addxiancheng(u.getUserId(), xc);
//                                        xc.start();
//                                        xc.tongzhi(u.getUserId());
//                                        System.out.println(u.getUserId()+"客户机连接成功");
//                                        JOptionPane.showMessageDialog(null,"成功");
//                                        Toolkit.getDefaultToolkit().beep();
//                                        userlist.add(u.getUserId());
//                                        lb.upateFriend(m);
//                                    }
                                }
                            } catch (Exception e)
                            {}
                        }
                    }).start();
                }
            }
        });
        send_Btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });
    }

    public JTextArea getShow_Mes() {
        return show_Mes;
    }

    public void setShow_Mes(JTextArea show_Mes) {
        this.show_Mes = show_Mes;
    }

    public static void main(String[] args) {
      //  WebLookAndFeel.install ();
        JFrame frame = new JFrame("Server_Interface");
        frame.setContentPane(new Jiemian().Jpanel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setBounds(((Toolkit.getDefaultToolkit().getScreenSize().width)/2)-300,
                ((Toolkit.getDefaultToolkit().getScreenSize().height)/2)-300,600,600);
        frame.setSize(600,600);
        User u=new User();
        u.setUserId("1");
        u.setPasswd(new String("123123"));
        lb=new liebiao(u.getUserId(),"0");
        Glhylb.addliebiao(u.getUserId(),lb);
        lb.setBounds(((Toolkit.getDefaultToolkit().getScreenSize().width)/2)+300,
                ((Toolkit.getDefaultToolkit().getScreenSize().height)/2)-300,600,600);
        lb.setSize(200,590);
    }

}